/*
 * 包含grid呈现管理的主体功能
 */

var JieGrid = function(aoParent, aiWidth, aiHeight, aiTop, aiLeft){

	if(aoParent == null || aoParent.nodeType != 1)	throw new Error(sGmsg["GRID_001"]);
	
	JieElement.call(this);//继承JieElement类的属性
	this.elParent = aoParent;
	
	if(aiWidth == null || aiHeight === null) throw new Error(sGmsg["GRID_002"]);
	this.iWidth = Util.getLengthNumber(aiWidth);
	this.iHeight = Util.getLengthNumber(aiHeight);
	this.makeElement(Properties.system.GRID_TAG, this.iWidth, this.iHeight);
	this.elParent.appendChild(this.element);

	aiTop == null ? this.iTop = 0 : this.iTop = aiTop;
	aiLeft == null? this.iLeft = 0 : this.iLeft = aiLeft;
	
	this.rightScroll = null;
	this.bottomScroll = null;
		
	this.FrameElements = new JieFrame(this);
	
	this.iFixed = 1;
	this.iHeadCnt = 1;
	this.iFixedAreaWidth = 0;
	this.iDataAreaWidth = 0;
	this.iDataAreaHeight = 0;
	this.rowHeight = Util.getLengthNumber(Properties.grid.row.height);
	this.headerRowHeight = Util.getLengthNumber(Properties.grid.header.height);
	
	this.headers = null;
	this.rows = new Array();
	this.columns = new Array();
//============================================= 私有方法 ====================================================

	this.makeHeader = function(aoColumn, aoTemp){
		var arrTitles = aoColumn.title, j=0;
		for(var i=0; i<this.iHeadCnt; i++){
			var oCell = new JieHeadCell(aoColumn, this.headers[i]);
			oCell.setValue(arrTitles[i])
			oCell.setBgColor(aoColumn.headBgColor);
			oCell.setBorderColor(aoColumn.headBgColor);
		//	alert(this.dateForeColor);
			oCell.setForeColor(aoColumn.headForeColor);
			oCell.setFontFamily(aoColumn.headFontFamily);
			oCell.setFontSize(aoColumn.headFontSize?aoColumn.headFontSize:Properties.grid.cell["font-size"]);
			oCell.setAlign("center");
			oCell.setBorderStyle("outset");
			/*
			alert(arrTitles[i]);
			if (aoTemp[i] != null) alert("temp: " + aoTemp[i].value);
			else alert("null");*/
			if (arrTitles[i] == null || arrTitles[i] == arrTitles[i - 1]) {
				aoTemp[i] = oCell;
				if (i > 0) {
//					alert("merge.portrait");
					aoTemp[i - 1].mergeCell(Properties.system.merge.portrait, oCell);
					if (i != this.iHeadCnt - 1) {
						aoTemp[i] = aoTemp[i - 1];
					}
					else {
						aoTemp[i] = null;
					}
					aoTemp[i - 1] = null;
				}
			}
			else {
//				alert(aoTemp[i].getValue());
				if (aoTemp[i] != null && arrTitles[i] == aoTemp[i].getValue()) {
//					alert("merge.landscape");
					aoTemp[i].mergeCell(Properties.system.merge.landscape, oCell, aoColumn);
				}else{
//					alert("merge.none");
					aoTemp[i] = oCell;
				}
			}
			
			this.headers[i].appendCell(oCell);
			
		}
		return aoTemp;
	};

}

JieGrid.prototype = JieElement.prototype;//继承JieElement类的共有方法

//============================================= 公有方法 ====================================================

/**
 * 设置grid表头头行数，该方法只在grid初始化之前有意义，一旦初始化结束，该方法将失效(暂时这么用)
 * 
 * @param {int} aiCount 表头行数，如果不是数字，函数将抛出异常
 */
JieGrid.prototype.setHeadersCount = function(aiCount){
	if(isNaN(aiCount)) throw new Error(sGmsg["GRID_004"]);
	this.iHeadCnt = aiCount;
};
JieGrid.prototype.setFixedCount = function(aiCount){
	if(isNaN(aiCount)) throw new Error(sGmsg["GRID_004"]);
	this.iFixed = aiCount;
};
JieGrid.prototype.buildEvent = function(aoColumn){
	var oEventNames = Properties.system.eventType;
	aoColumn.appendListener(oEventNames["click"], clickProcessor.editCell);
	aoColumn.appendListener(oEventNames["click"], clickProcessor.selectRow);
	aoColumn.appendListener(oEventNames["blur"], blurProcessor.closeCell);
	aoColumn.appendListener(oEventNames["blur"], blurProcessor.clearRowColor);
	aoColumn.appendListener(oEventNames["doubleclick"]);
		
}
/**
 * 初始化grid。按照配置好的属性，根据格式化模板将grid显示出来
 * @param {Object} alFormatTems 格式化模板
 */
JieGrid.prototype.initGrid = function(alFormatTems){
	if(alFormatTems == null ) throw new Error(sGmsg["GRID_003"]);
	
	var jco = null;
	
	//初始化表头数组
	this.headers = new Array(this.iHeadCnt);
	for(var i=0; i<this.iHeadCnt; i++) this.headers[i] = new JieRow(this, this.headerRowHeight, this.FrameElements.FIXED_HEADER.DATA_BOX, this.FrameElements.HEADER.DATA_BOX);
	var aTitleTemp = new Array(this.iHeadCnt);//缓存上一列表头数据
	
	for(var i=0; i<alFormatTems.length; i++){
		var alFormatTem = alFormatTems[i];
		jco = new JieColumn(alFormatTem);
		this.buildEvent(jco);
		if (this.iFixed > i) {
			this.iFixedAreaWidth += jco.iWidth+2;
			jco.isFixed = true;
		} else if(this.iFixed == i){
			aTitleTemp.splice(0, this.iHeadCnt);
			this.iDataAreaWidth += jco.iWidth+2;
			jco.isFixed = false;
		}else{
			this.iDataAreaWidth += jco.iWidth+2;
			jco.isFixed = false;
		}
		aTitleTemp = this.makeHeader(jco, aTitleTemp);
		
		this.columns.push(jco);
		
	}
	for(var i=0; i<this.headers.length; i++){
		this.headers[i].addRowToGrid();
	}
	var iHeaderHeight = this.iHeadCnt*this.headerRowHeight+(this.iHeadCnt-1);
	var iScrollWidth = Util.getLengthNumber(Properties.grid.scrollWidth);
	var iDataFrameWidth = this.iWidth - this.iFixedAreaWidth - iScrollWidth;
	this.iDataAreaHeight = this.iHeight - iHeaderHeight - iScrollWidth;
	//aiFixedAreaWidth, aiHeaderHeight, aiDataAreaWidth, aiDataFrameWidth, aiScrollWidth, aiDataAreaHeight
	this.FrameElements.makeFrame(this.iFixedAreaWidth, iHeaderHeight, this.iDataAreaWidth, iDataFrameWidth, iScrollWidth, this.iDataAreaHeight);
	
};
JieGrid.prototype.getColById = function(asColumnId){
	for(var i=0; i<this.columns.length; i++){
		if (this.columns[i].id == Util.trim(asColumnId)) {
			return this.columns[i];
		}
	}
}
JieGrid.prototype.insertRow = function(){
	var oRow = new JieRow(this, this.headerRowHeight, this.FrameElements.FIXED_DATA.DATA_BOX, this.FrameElements.DATA_AREA.DATA_BOX);
	var iDataAreaHeight = Util.getLengthNumber(this.FrameElements.DATA_AREA.DATA_BOX.style.height);
	iDataAreaHeight += Util.getLengthNumber(oRow.getHeight());
	this.FrameElements.FIXED_DATA.DATA_BOX.style.height =this.FrameElements.PORTRAIT_SCROLL.DATA_BOX.style.height =this.FrameElements.DATA_AREA.DATA_BOX.style.height = iDataAreaHeight+"px";
	oRow.insertRowToGrid();
//	this.FrameElements.moveScrollLandscape();
}

